import org.junit.Test;

import java.lang.annotation.ElementType;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/3/31 14:20
 * 说明：获取Class的方法
 */

public class ClassTest {
    @Test
    public void test1() throws ClassNotFoundException {
        // 1. 调用运行时类的静态属性：class
        Class class1 = User.class;

        // 2. 调用运行时类的对象的getClass()
        User u1 = new User();
        Class class2 = u1.getClass();

        System.out.println(class1 == class2);   // true

        // 3. 调用Class的静态方法forName(String className)
        String className = "User";
        Class<?> class3 = Class.forName(className);
        System.out.println(class1 == class3); // true
        System.out.println(class2 == class3); // true

        // 运行时类在内存中会缓存起来，在整个执行期间，只加载一次。
    }

    @Test
    public void test2() {
        Class c1 = Object.class;
        Class<Comparable> c2 = Comparable.class;
        Class<String[]> c3 = String[].class;
        Class<int[][]> c4 = int[][].class;
        Class<ElementType> c5 = ElementType.class;
        Class<Override> c6 = Override.class;
        Class<Integer> c7 = int.class;
        Class<Void> c8 = void.class;
        Class<Class> c9 = Class.class;

        int[] a = new int[10];
        int[] b = new int[100];
        Class c10 = a.getClass();
        Class c11 = b.getClass();

        // 只要元素类型和维度一样，就认为是同一种Class
        System.out.println(c10 == c11);

    }
}
